package com.wyw.rabbit.learning;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Title   通配符模式的生产者，发送消息
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * @Date 2024/3/29 11:25
 * Update History:
 * Author        Time            Content
 */
public class TopicsProducer {

    public static void main(String[] args) throws Exception {

        // 1、创建连接工厂以及相关的参数配置
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/");
        factory.setUsername("wyw");
        factory.setPassword("wyw");

        // 2、通过工厂创建连接
        Connection connection = factory.newConnection();

        // 3、创建管道
        Channel channel = connection.createChannel();

        // 4、创建交换机
        /**
         * exchange     交换机名称
         * type         交换机类型
         *              DIRECT("direct"),   定向投递模式（把消息发送给指定的"routing key"的队列）
         *              FANOUT("fanout"),   广播模式（发布订阅，把消息发送给所有的绑定队列）
         *              TOPIC("topic"),     通配符模式（把消息发送给符合的"routing pattern"的队列）
         *              HEADERS("headers"); 使用率不高（参数匹配）
         * durable      是否持久化
         * autoDelete   是否自动删除
         * internal     内部意思，true代表当前exchange是rabbitmq内部使用的，用户创建的队列不会消费该类型交换机下的消息，所以一般是false
         * arguments    map类型的参数
         */
        String topics_exchange = "topics_exchange";
        channel.exchangeDeclare(topics_exchange,
                BuiltinExchangeType.TOPIC,
                true, false, false, null);

        // 5、创建队列 Queue（简单模式不需要交换机Exchange）
        String topics_queue_order = "topics_queue_order";
        String topics_queue_pay = "topics_queue_pay";
        channel.queueDeclare(topics_queue_order, true, false, false, null);
        channel.queueDeclare(topics_queue_pay, true, false, false, null);

        // 6、绑定交换机和队列
        // *一个    #零个或多个
        channel.queueBind(topics_queue_order, topics_exchange, "order.*");
        channel.queueBind(topics_queue_pay, topics_exchange, "*.pay.#");

        String msg1 = "创建订单A";
        String msg2 = "创建订单B";
        String msg3 = "删除订单C";
        String msg4 = "修改订单D";
        String msg5 = "支付订单E";
        String msg6 = "超市订单F";
        String msg7 = "慕课订单G";
        channel.basicPublish(topics_exchange, "order.create", null, msg1.getBytes());
        channel.basicPublish(topics_exchange, "order.create", null, msg2.getBytes());
        channel.basicPublish(topics_exchange, "order.delete", null, msg3.getBytes());
        channel.basicPublish(topics_exchange, "order.update", null, msg4.getBytes());
        channel.basicPublish(topics_exchange, "order.pay", null, msg5.getBytes());
        channel.basicPublish(topics_exchange, "wyw.pay.super.market", null, msg6.getBytes());
        channel.basicPublish(topics_exchange, "wyw.pay.course", null, msg7.getBytes());
        // 8、 释放资源
        channel.close();
        connection.close();

    }
}
